package com.polytech.diploma.SQLGenerator.Oracle.Statements;

import java.util.List;
import java.util.Map;

import com.polytech.diploma.SQLGenerator.Oracle.Conditions.Condition;

public class JoinStatement implements Subquery {

	Table leftTable = null;
	Table rightTable = null;
	
	JoinType type;
	
	List<Condition> conditions = null;
	
	private JoinStatement(Builder builder) {
		super();

		this.leftTable = builder.leftTable;
		this.rightTable = builder.rightTable;
		this.type = builder.type;
		this.conditions = builder.conditions;
	}
	
	public static class Builder {
		Table leftTable;
		Table rightTable;		
		JoinType type;		
		List<Condition> conditions;
		
		public Builder setLeftTable(Table leftTable) {
			this.leftTable = leftTable;
			return this;
		}
		public Builder setRightTable(Table rightTable) {
			this.rightTable = rightTable;
			return this;
		}
		public Builder setType(JoinType type) {
			this.type = type;
			return this;
		}
		public Builder setConditions(List<Condition> conditions) {
			this.conditions = conditions;
			return this;
		}	
		public JoinStatement build()
		{
			return new JoinStatement(this);		
		}
	}

	public Table getLeftTable() {
		return leftTable;
	}

	public Table getRightTable() {
		return rightTable;
	}

	public JoinType getType() {
		return type;
	}

	public List<Condition> getConditions() {
		return conditions;
	}

	@Override
	public Map<String, List<String>> getTablesWithFields(
			Map<String, List<String>> fieldMap) {

		if(leftTable != null)
			fieldMap = leftTable.getTablesWithFields(fieldMap);
		if(rightTable != null)
			fieldMap = rightTable.getTablesWithFields(fieldMap);		
		
		if(conditions == null)
			return fieldMap;
		
		for(Condition condition : conditions)
		{
			fieldMap = condition.getTablesWithFields(fieldMap);
		}
		
		return fieldMap;
	}	
	
}
